package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.guli.service.base.dto.CourseInfoDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.CourseSearch;
import com.atguigu.guli.service.edu.entity.vo.CourseDetailsVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-27
 */
@RestController

@RequestMapping("/api/edu/course")
public class ApiCourseController {
    @Autowired
    CourseService courseService;
    @Autowired
    TeacherService teacherService;
    //5、更新课程销量
    @PutMapping("updateSales/{courseId}")
    public R updateSales(@PathVariable String courseId){
        Course course = courseService.getById(courseId);
        course.setBuyCount(course.getBuyCount()+1);
        courseService.updateById(course);
        return R.ok();
    }
    //4、订单微服务 远程 根据课程id查询课程详情:课程详情对应的bean 远程涉及的消费者和生产者都需要使用该类
    //我们可以定义一个DTO类 描述远程访问传递的数据： data transfer object,该类抽取到公共模块中[service-base]
    @GetMapping("getCourseDto/{courseId}")
    public R getCourseDto(@PathVariable String courseId){
        Course course = courseService.getById(courseId);
        Teacher teacher = teacherService.getById(course.getTeacherId());
        CourseInfoDto dto = new CourseInfoDto();
        BeanUtils.copyProperties(course,dto);
        dto.setTeacherName(teacher.getName());
        return R.ok().data("item",dto);
    }
    //3.根据课程id查询课程详情页数据
    @GetMapping("getCourseDetails/{courseId}")
    public R getCourseDetails(@PathVariable String courseId){
        CourseDetailsVo vo = courseService.getCourseDetails(courseId);
        return R.ok().data("item",vo);
    }
    //2、根据条件查询课程列表
    @GetMapping("list")
    public R listCourses(CourseSearch courseSearch){
        List<Course> courses = courseService.listCourses(courseSearch);
        return R.ok().data("items" , courses);
    }
    //1.根据讲师id查询他讲过的课程列表
    @GetMapping("getCourses/{teacherId}")
    public R getCourseByTeacherId(@PathVariable String teacherId){
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Course::getId,Course::getCover , Course::getTitle);
        queryWrapper.eq(Course::getTeacherId , teacherId);
        List<Course> courses = courseService.list(queryWrapper);
        return R.ok().data("items" , courses);
    }

}

